#include "ClientConfig.h"
#include <irrlicht.h>
#include <iostream>
#include "MBSTrace.h"

// include the gui creator element factory
#include "CGUIEditFactory.h"

using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

int main()
{
    ClientConfig config;

    config.load("client.xml");

    IrrlichtDevice *device = createDevice( 	config.video.driver,
                                            dimension2d<s32>(config.video.horizontal,config.video.vertical),
                                            config.video.bpp,
                                            config.video.fullscreen,
                                            false,	//stencilbuffer (shadows)
                                            false);	//vsync

    if (device==NULL)
    {
        TRACE_ERROR("NULL device");
        return -1;
    }

	IGUIEnvironment *env = device->getGUIEnvironment();
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    /*
    	first we create the factory which can make new GUI elements
    	and register it with the gui environment.
    */

    IGUIElementFactory* factory = new CGUIEditFactory(env);
    env->registerGUIElementFactory(factory);
    // remember to drop since we created with a create call
    factory->drop();

    IGUISkin *skin = env->createSkin(EGST_WINDOWS_METALLIC);
    env->setSkin(skin);

    IGUIFont *font = env->getFont("media/lucida.xml");
    if (font)
        skin->setFont(font);
    skin->drop();

    // change transparency of skin
    for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
    {
        video::SColor col = env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i);
        col.setAlpha(250);
        env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
    }

    /*
    	now we add the GUI Editor Workspace
    */

    env->loadGUI("gui.xml");

    env->addGUIElement("GUIEditor");

    while (device->run())
    {
        device->sleep(50);

        if (device->isWindowActive())
        {
            driver->beginScene(true, true, video::SColor(0,200,200,200));
            smgr->drawAll();
            env->drawAll();
            driver->endScene();
        }
        else
			device->sleep(200);
    }

    device->drop();

    return 0;
}


